package com.ctrlspace.servlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.CometEvent;
import org.apache.catalina.CometProcessor;
import org.apache.catalina.CometEvent.EventType;

public class CometConnection extends HttpServlet implements CometProcessor {
	private static final long serialVersionUID = 7044405265203183426L;

	public CometConnection() {
		System.err.println("Comet test servlet initialized.");
	}

	public void init() {
		System.err.println("Comet test initialized.");
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		System.err.println("doGet called.");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		System.err.println("doPost called.");
	}

	public void event(CometEvent cometEvent) throws IOException,
			ServletException {
		System.err.println("Received event "
				+ cometEvent.getEventType().toString());

		if (cometEvent.getEventType() == EventType.BEGIN) {
			String jdk = "C:\\Program Files\\Java\\jdk1.6.0_16\\bin\\";
			String wawa = jdk
					+ "java.exe -cp \"F:\\Google AppEngine\\appengine-java-sdk-1.2.6\\lib\\appengine-tools-api.jar\" com.google.appengine.tools.admin.AppCfg --passin --proxy=proxy.dlsu.edu.ph:80 --proxy_https=proxy.dlsu.edu.ph:80 update \"F:\\Google AppEngine\\appengine-java-sdk-1.2.6\\demos\\guestbook\\war\"";
			Process p = Runtime.getRuntime().exec(wawa);

			// any error message?
			StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(),
					"ERROR", cometEvent);

			// any output?
			StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(),
					"OUTPUT", cometEvent);

			// kick them off
			errorGobbler.start();
			outputGobbler.start();
		}
	}

	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response
				.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
						"You must be using the APR or NIO connector to get the event method called.");
	}

	private class StreamGobbler extends Thread {
		InputStream is;
		String type;
		CometEvent event;

		StreamGobbler(InputStream is, String type, CometEvent event) {
			this.is = is;
			this.type = type;
			this.event = event;
		}

		public void run() {
			System.out.println("wa");
			try {
				InputStreamReader isr = new InputStreamReader(is);
				BufferedReader br = new BufferedReader(isr);
				String line = null;
				while ((line = br.readLine()) != null) {
					event.getHttpServletResponse().getWriter().flush();
					event.getHttpServletResponse().getWriter().println(type + "> " + line);
					System.out.println(type + "> " + line);
				}
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
			
			try {
				if (event != null)
					event.close();	
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}